<template>
  <section>
    <el-button size="mini" @click="addTypeBtn" style="float: right" type="primary">新建类型</el-button>
    <el-table :data="typeList">
      <el-table-column type="index" label="序号"/>
      <el-table-column label="名称" prop="name">
        <template slot-scope="scope">
          <el-input v-if="scope.row.edit" size="mini" v-model="scope.row.name"></el-input>
          <span v-if="!scope.row.edit">{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="编码" prop="code">
        <template slot-scope="scope">
          <el-input v-if="scope.row.edit" size="mini" v-model="scope.row.code"></el-input>
          <span v-if="!scope.row.edit">{{scope.row.code}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button :type="scope.row.edit?'success':'primary'" @click='confirm(scope.row)' size="mini" icon="edit">{{scope.row.edit?'完成':'编辑'}}</el-button>
          <el-button @click='cancelBtn(scope)' size="mini" v-if="scope.row.edit">取消</el-button>
          <el-button @click='del(scope)' size="mini" v-if="!scope.row.edit" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </section>
</template>

<script>
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import errHandler from '@/assets/errHandler.js'
  import Vue from 'vue'//后续添加属性，需要vue联动
  export default {
    name: "codeTypes",
    props:[],
    components:{},
    data() {
      return {
        typeList:null
      }
    },
    created(){
      //加载二维码类型列表
      this.getQrCodeTypeList()
    },
    computed: {
      ...mapGetters(['department']),
    },
    watch:{},
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      addTypeBtn(){
        this.typeList.push({title:'',code:'',edit:true})
      },
      getQrCodeTypeList(){
        this.fetch({name:"getQrCodeTypeList"})
          .then(res=>{
            console.log(res)
            this.typeList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      createQrCodeType(row){
        this.fetch({name:"createQrCodeType",data:row})
          .then(res=>{
            console.log('创建成功——————',res)
            this.getQrCodeTypeList()
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      confirm(row){
        if(row.edit === undefined){
          Vue.set(row,'edit',true)//后续添加属性，需要vue联动
          return
        }
        if(row.edit){
          this.loading=true
          if(row.id){
            this.updateQrCodeType(row)
          }else {
            this.createQrCodeType(row)
          }
        }else {
          row.edit=true
        }
      },
      cancelBtn(scope){//取消按钮
        if(scope.row.edit === undefined){
          Vue.set(scope.row,'edit',false)//后续添加属性，需要vue联动
        }else {
          scope.row.edit =false
        }
        if(scope.row.id === undefined){
          this.typeList.splice(scope.$index,1)
        }
      },
      del(scope){//删除按钮
        this.loading=true
        this.$confirm('确认删除？')
          .then(()=> {
            this.fetch({name:"deleteQrCodeType",data:{id:scope.row.id}})
              .then(res=>{
                this.loading=false
                this.typeList.splice(scope.$index,1)
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
            this.loading = false
          })
      },
      updateQrCodeType(row){
        this.fetch({name:"updateQrCodeType",data:{...row}})
          .then(res=>{
            this.loading=false
            console.log(res)
            row.edit=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      }
    }
  }
</script>

<style scoped>

</style>
